<template>
    <div class="ldm-info">
        <div class="page-header">
            <a-button type="link" class="back" @click="goBack">
                <LeftOutlined class="back-icon" /> 返回
            </a-button>
            <span class="sep">|</span>
            <span class="title">详情</span>
        </div>

        <div class="content-area">
            <div class="body" v-if="detail">
                <div class="content">
                    <div class="row">
                        <div class="label">查询关键词：</div>
                        <div class="value">{{ detail.query || '-' }}</div>
                    </div>
                    <div class="row">
                        <div class="label">标题：</div>
                        <div class="value">{{ detail.title || '-' }}</div>
                    </div>
                    <div class="row">
                        <div class="label">链接：</div>
                        <div class="value"><span class="url-text">{{ detail.page_url || '-' }}</span></div>
                    </div>
                    <div class="row">
                        <div class="label">频道：</div>
                        <div class="value">{{ detail.sence_label || detail.sence || '-' }}</div>
                    </div>
                    <div class="row">
                        <div class="label">地理位置：</div>
                        <div class="value">{{ detail.position || '-' }}</div>
                    </div>
                    <div class="row">
                        <div class="label">摘要：</div>
                        <div class="value">{{ detail.summary || '-' }}</div>
                    </div>
                    <div class="row">
                        <div class="label">启用状态：</div>
                        <div class="value">
                            <a-radio-group :value="detail.status === 'published' ? 'enabled' : 'disabled'"
                                :disabled="true">
                                <a-radio value="enabled">启用</a-radio>
                                <a-radio value="disabled">停用</a-radio>
                            </a-radio-group>
                        </div>
                    </div>
                    <div class="row">
                        <div class="label">创建时间：</div>
                        <div class="value">{{ formatDateTime(detail.date_created) || '-' }}</div>
                    </div>
                </div>
            </div>

            <div v-else class="empty">加载中…</div>

            <div class="ldm-fixed-footer">
                <a-button type="link" class="link btn primary" @click="goEdit">编辑</a-button>
                <span class="sep">|</span>
                <a-button type="link" class="link btn ghost" @click="goBack">返回</a-button>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { LeftOutlined } from '@ant-design/icons-vue'
import { getLinkInsertDetail, type LinkInsertDetail } from '@/api/modules/linkInsert.api'

const router = useRouter()
const route = useRoute()
const TAB_KEY = 'linkInsertion'
const id = route.query.id ? String(route.query.id) : ''

const detail = ref<LinkInsertDetail | null>(null)

function formatDateTime(input?: string | Date | null): string {
    if (!input) return ''
    const raw = typeof input === 'string' ? input.trim() : input
    if (typeof raw === 'string' && /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(raw)) return raw
    const d = new Date(raw as any)
    if (isNaN(d.getTime())) return String(input)
    const pad = (n: number) => String(n).padStart(2, '0')
    return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
}

async function init() {
    try {
        detail.value = await getLinkInsertDetail(id)
    } catch (e: any) {
        message.error(e?.message || '加载失败')
    }
}
onMounted(init)

function backTarget() {
    const backTo = route.query.backTo
    const from = route.query.from as string | undefined
    if (backTo === 'tab' && from) return { path: from, query: { tab: TAB_KEY } }
    return { path: '/linkInsertion' }
}
function goBack() { router.replace(backTarget()) }

function goEdit() {
    if (!detail.value) return
    router.push({
        path: '/linkInsertionPage',
        query: {
            ...(route.query.backTo ? { backTo: 'tab', from: route.query.from, tab: TAB_KEY } : {}),
            mode: 'edit',
            id: String(detail.value.id),
            row: encodeURIComponent(JSON.stringify(detail.value)),
        },
    })
}
</script>

<style lang="less" scoped>
@pad-x: 24px;
@wrap-max: 640px;
@label-w: 96px;
@safe-bottom: env(safe-area-inset-bottom);

.ldm-info {
    background: #fff;
    border: 1px solid #eaeaea;
    border-radius: 6px;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.page-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 24px @pad-x;
    border-bottom: 1px solid #f0f0f0;
}

.back.ant-btn {
    display: inline-flex;
    align-items: center;
    padding-left: 24px;
    height: 24px;
    line-height: 24px;
    font-size: 14px;
    color: #81889c;
}

.back-icon {
    font-size: 16px;
}

.sep {
    color: #d9d9d9;
}

.title {
    font-size: 16px;
    font-weight: 500;
    color: #000;
}

.content-area {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: auto;
    padding-bottom: calc(16px + @safe-bottom);
}

.body {
    padding: 12px 0 0;
}

.content {
    padding: 8px @pad-x 12px 140px;
    max-width: @wrap-max;
}

.row {
    display: flex;
    align-items: center;
    min-height: 44px;
    padding: 6px 0;
    border-bottom: 1px solid #efefef;
}

.row:last-child {
    border-bottom: 0;
}

.label {
    width: @label-w;
    color: #666;
    flex: 0 0 auto;
    text-align: left;
}

.value {
    flex: 1 1 auto;
    color: #333;
    word-break: break-all;
}

.url-text {
    color: #333;
    user-select: text;
}

.empty {
    padding: 40px;
    text-align: center;
    color: #999;
}

.ldm-fixed-footer {
    position: static;
    margin-top: 16px;
    padding: 12px @pad-x calc(12px + @safe-bottom) 36px;
    background: #fff;
    border-top: 1px solid #eaeaea;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    flex-wrap: wrap;
}

.link.btn {
    font-size: 14px;
    height: 36px;
    padding: 0 20px;
    line-height: 36px;
    border-radius: 8px;
    font-weight: 600;
}

.primary {
    background: #1677ff !important;
    border: 1px solid #1677ff !important;
    color: #fff !important;
}

.ghost {
    background: #fafafa !important;
    border: 1px solid #e8e8e8 !important;
    color: #666 !important;
}
</style>
